package com.dexilaonian.ui;

import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.dexilaonian.event.KillEvent;
import com.dexilaonian.utils.SystemBarTintManager;
import com.dexilaonian.view.LoadingDialog;

import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;


public abstract class BaseActivity extends AppCompatActivity {

    private View layoutInflater;
    private LoadingDialog dialog;
    private Toast toast;
    public static Activity currentActivity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTranslucentStatus();
        layoutInflater = LayoutInflater.from(this).inflate(getLayoutID(), null);
        setContentView(layoutInflater);
        ButterKnife.bind(layoutInflater, this);
        dialog = new LoadingDialog(this);
        EventBus.getDefault().register(this);
        initView();
        initData();
    }

    public void onEventMainThread(KillEvent event) {
        finish();
    }

    /**
     * 设置状态栏背景状态
     */
    @TargetApi(19)
    private void setTranslucentStatus() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window win = getWindow();
            WindowManager.LayoutParams winParams = win.getAttributes();
            final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
            winParams.flags |= bits;
            win.setAttributes(winParams);
        }
        SystemBarTintManager tintManager = new SystemBarTintManager(this);
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setStatusBarTintResource(0);//状态栏无背景
    }

    public abstract int getLayoutID();

    public abstract void initView();

    protected abstract void initData();

    //等待圈圈
    public void showProgressBar(Boolean isShow) {
        if (isShow) {
            dialog.show();
        } else {
            if (dialog != null) {
                dialog.dismiss();
            }
        }
    }

    public void hideProgressBar() {
        if (dialog != null) {
            dialog.dismiss();
        }
    }

/*    public void showToast(String msg) {
        if (TextUtils.isEmpty(msg)) {
            return;
        } else {
            if (toast == null) {
                toast = Toast.makeText(BaseActivity.this, msg, Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER,0,0);
            } else {
                toast.setText(msg);
            }
            toast.show();
        }
    }*/

    public void showToast(final String msg) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (TextUtils.isEmpty(msg)) {
                    return;
                } else {
                    if (toast == null) {
                        toast = Toast.makeText(BaseActivity.this, msg, Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                    } else {
                        toast.setText(msg);
                    }
                    toast.show();
                }
            }
        });
    }
    /* public void showToast(int msg) {
            if (toast == null) {
                toast = Toast.makeText(this, getResources().getString(msg), Toast.LENGTH_SHORT);

            } else {
                toast.setText(msg);
            }
            toast.show();
        }*/
    @Override
    protected void onResume() {
        super.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
        hideToast();
    }

    public void hideToast() {
        if (toast != null) {
            toast.cancel();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
        EventBus.getDefault().unregister(this);
    }
}
